package com.homer.gps.track.helper;

import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;

import com.homer.gps.track.R;

public class LocaleDelegate {
	/**
	 * The country preference key.
	 */
	private static final String PREFERENCES_KEY_LOCALE = "locale";

	/**
	 * The language preference key.
	 */
	private static final String PREFERENCES_KEY_LANGUAGE = "language";

	/**
	 * The country preference key.
	 */
	private static final String PREFERENCES_KEY_COUNTRY = "country";

	/**
	 * Creates a new locale from the given locale id.
	 * 
	 * @param localeId
	 *            the locale id
	 */
	public static Locale createLocaleFromId(String localeId) {
		int dividerIndex = localeId.indexOf("_");
		if (dividerIndex != -1) {
			String language = localeId.substring(0, dividerIndex);
			String country = localeId.substring(dividerIndex + 1,
					localeId.length());
			return new Locale(language, country);
		} else {
			throw new IllegalArgumentException(
					"locale id has a wrong format : " + localeId);
		}
	}

	/**
	 * Creates a new locale id from the given locale.
	 * 
	 * @param locale
	 *            the locale
	 */
	public static String createIdFromLocale(Locale locale) {
		StringBuffer resultBuffer = new StringBuffer();
		resultBuffer.append(locale.getLanguage());
		resultBuffer.append('_');
		resultBuffer.append(locale.getCountry());
		return resultBuffer.toString();
	}
	
	public static Locale getAcceptedLocale(Context context, Locale locale) {
		String[] acceptedLocaleIds = context.getResources().getStringArray(R.array.settings_languages_values);
		String localeId = createIdFromLocale(locale);
		for (String acceptedLocaleId : acceptedLocaleIds) {
			if(localeId.equals(acceptedLocaleId)) {
				return locale;
			}
		}
		
		return new Locale("en","GB");
	}

	/**
	 * Sets the given locale in the shared preferences.
	 * 
	 * @param context
	 *            the context
	 * @param locale
	 *            the locale
	 */
	public static void setPreferenceLocale(Context context, Locale locale) {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor preferencesEditor = preferences.edit();
		preferencesEditor.putString(PREFERENCES_KEY_LOCALE,
				LocaleDelegate.createIdFromLocale(locale));
		preferencesEditor.putString(PREFERENCES_KEY_LANGUAGE,
				locale.getLanguage());
		preferencesEditor.putString(PREFERENCES_KEY_COUNTRY,
				locale.getCountry());
		preferencesEditor.commit();
	}

	/**
	 * Returns the locale from the shared preferences.
	 * 
	 * @param context
	 *            the context
	 * @return the locale
	 */
	public static Locale getPreferenceLocale(Context context) {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		String preferencesLanguage = preferences.getString(
				PREFERENCES_KEY_LANGUAGE, null);
		String preferencesCountry = preferences.getString(
				PREFERENCES_KEY_COUNTRY, null);
		if (preferencesLanguage != null && preferencesCountry != null) {
			return new Locale(preferencesLanguage, preferencesCountry);
		} else {
			return null;
		}
	}

	/**
	 * Returns true if the given locale is stored in the shared references.
	 * 
	 * @param context
	 *            the context
	 * @param locale
	 *            the locale
	 * @return true if the given locale is stored otherwise false
	 */
	public static boolean isPreferenceLocale(Context context, Locale locale) {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		String preferencesLanguage = preferences.getString(
				PREFERENCES_KEY_LANGUAGE, Locale.getDefault().getLanguage());
		String preferenceCountry = preferences.getString(
				PREFERENCES_KEY_COUNTRY, Locale.getDefault().getCountry());
		return locale.getLanguage().equals(preferencesLanguage) && locale.getCountry().equals(preferenceCountry);
	}

	/**
	 * Returns true if the locale in the configuration is different from the
	 * given locale.
	 * 
	 * @param context
	 *            the context
	 * @param locale
	 *            the locale
	 * @return true if the locale in the configuration is different otherwise
	 *         false
	 */
	public static boolean configurationLocaleChanged(Context context,
			Locale locale) {
		Resources resources = context.getResources();
		Configuration configuration = resources.getConfiguration();
		return !locale.equals(configuration.locale);
	}

	/**
	 * Updates the configuration to the given locale.
	 * 
	 * @param context
	 *            the context
	 * @param locale
	 *            the locale
	 */
	public static void updateConfiguration(Context context, Locale locale) {
		Resources resources = context.getResources();
		Configuration configuration = resources.getConfiguration();
		if (!locale.equals(configuration.locale)) {
			Configuration newConfiguration = new Configuration(configuration);
			newConfiguration.locale = locale;
			resources.updateConfiguration(newConfiguration,
					resources.getDisplayMetrics());
		}
	}

	/**
	 * Returns the locale from the configuration.
	 * 
	 * @param context
	 *            the context
	 * @return the locale
	 */
	public static Locale getConfigurationLocale(Context context) {
		Resources resources = context.getResources();
		Configuration configuration = resources.getConfiguration();
		return configuration.locale;
	}
}
